﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace Reece.Open.Mvc.Atom.Factories
{
    public static class TransformerFactory
    {
        private static ConcurrentDictionary<Type, Func<object, string>> _methods = new ConcurrentDictionary<Type, Func<object, string>>();

        static TransformerFactory()
        {
            _methods = new ConcurrentDictionary<Type, Func<object, string>>();
            _methods.TryAdd(typeof (DateTime), o => XmlConvert.ToString((DateTime) o, XmlDateTimeSerializationMode.Utc));
            _methods.TryAdd(typeof(object), o => o.ToString());
        }


        public static Func<object,string> GetTransformer(Type type)
        {
            Func<object,string> method;
            if ( ! _methods.TryGetValue(type, out method) || method == null )
            {
                _methods.TryGetValue(typeof (object), out method);
            }
            return method;
        }
    }
}
